<template>
	<view class="wrap" :style="colorStyle">
		<view class="tab_box flex_sb" v-if="!chooseAdd">
			<view class="tab_item flex_c" :class="{active:tabCurrent==index}" v-for="(item,index) in tabList"
				:key="index" @click="changeTab(index)">
				{{item.name}}
			</view>
		</view>
		<view class="list-box" v-if="dataList.length">
			<block v-for="(item,index) in dataList" :key="index">
				<view class="list">
					<!-- <view class="choos_icon">
						选择
					</view> -->
					<view @click="choosHandle(item)">
						<!-- 支付宝 -->
						<template  v-if="tabCurrent==0">
							<view class="name flex_s">
								{{item.alipay_name||''}}
								<!-- <view class="tip_box" v-if="item.exceed">
									未在配送范围
								</view> -->
							</view>
							<view class="span">
								<text>{{item.alipay_number||''}}</text>
							</view>
						</template>
						<!-- 银行卡 -->
						<template   v-else>
							<view class="name flex_s">
								{{item.bank_name||''}}
								<text>{{item.card_username||''}}</text>
								<!-- <view class="tip_box" v-if="item.exceed">
									未在配送范围
								</view> -->
							</view>
							<view class="span">
								<text>{{item.card_number||''}}</text>
							</view>
						</template>
						
						
					</view>

					<view class="btn-box flex_sb">
						<view class="left" @click="set_default(item.id)">
							<view class="img_box">
								<image src="../static/imgs/a2.png" v-if="item.is_default"></image>
								<image src="../static/imgs/a1.png" v-else></image>
							</view>
							设为默认
						</view>
						<view class="right">
							<view class="li" v-if="tabCurrent==0"
								@click="$util.JumpPath(`/pages/new_page/bank/aliInfo?id=${item.id}`)">
								<image src="../static/imgs/my-17.png"></image>
								编辑
							</view>
							<view class="li" v-else @click="$util.JumpPath(`/pages/new_page/bank/info?id=${item.id}`)">
								<image src="../static/imgs/my-17.png"></image>
								编辑
							</view>
							<view class="li" @click="openModal(item.id)">
								<image src="../static/imgs/my-18.png"></image>
								删除
							</view>
						</view>
					</view>
				</view>
			</block>
		</view>
		<emptyPage v-else></emptyPage>
		<view class="btn-d">
			<view class="btn" v-if="tabCurrent==0" @click="submitHandler">
				<text>添加支付宝</text>
			</view>
			<view class="btn" v-else @click="submitHandler">
				<text>添加银行卡</text>
			</view>
		</view>
		<modal ref="modal" content="是否确认删除?" confirmText="确定" @confirm="del"></modal>
		<authenticationMadal ref="authenticationMadalRef"></authenticationMadal>
	</view>
</template>
<script>
	import authenticationMadal from "@/components/authenticationModal/index.vue"
	import {
		getUserInfo
	} from '@/api/user.js';
	import colors from "@/mixins/color";
	import {
		debounceImmediate
	} from "@/utils/validate";
	import emptyPage from "@/components/emptyPage.vue";
	import modal from "@/components/modal/modal.vue";
	import {
		card_list,
		default_set,
		card_del
	} from "@/api/new_api.js";
	import {
		mapGetters
	} from "vuex";
	export default {
		components: {
			emptyPage,
			modal,
			authenticationMadal
		},
		mixins: [colors],
		data() {
			return {
				alterMain: false,
				dataList: [],
				delCode: '',
				chooseAdd: false,
				navigatorHeight: '',
				statusBarHeight: '',
				tabCurrent: 0,
				tabList: [{
					name: '支付宝',
					id: 2
				}, {
					name: '银行卡',
					id: 1
				}],
				authenticate_type:0
			};
		},
		async onLoad(option) {
			this.getSystemInfo()
			// const that = this;
			if (option.chooseAdd) {
				this.chooseAdd = true
				this.tabCurrent = option.tabCurrent
				this.changeNavTitle()
			}

		},
		onShow() {
			this.getUserInfo()
			this.card_list()
		},
		methods: {
			/*
			 * 获取用户信息
			 */
			getUserInfo: function() {
				let that = this;
				getUserInfo().then((res) => {
					const data=res.data
					this.authenticate_type=data.authenticate_type
				});
			},
			submitHandler(){
				if(this.authenticate_type!=1){
					return this.$refs.authenticationMadalRef.openHandle()
				}
				if(this.tabCurrent==0){
					// 支付宝
					this.$util.JumpPath('/pages/new_page/bank/aliInfo')
				}else{
					// 银行卡
					this.$util.JumpPath('/pages/new_page/bank/info')
				}
			},
			changeNavTitle(){
				let navTitle = '';
				if (this.tabCurrent == 0) {
					// 选择支付宝
					navTitle = "选择支付宝"
				} else {
					// 选择银行卡
					navTitle = "选择银行卡"
				}
				uni.setNavigationBarTitle({
					title: navTitle
				})
			},
			changeTab(e) {
				this.tabCurrent = e
				this.card_list()
			},
			getSystemInfo() {

				uni.getSystemInfo({
					success: res => {
						const system = res
						this.statusBarHeight = system.statusBarHeight //状态栏高度
						//获取胶囊信息
						const menu = uni.getMenuButtonBoundingClientRect()
						// this.searchLeft = this.menu.left //搜索按钮的右偏移量
						//导航栏高度= （胶囊顶部距离-状态栏高度） x 2 + 胶囊的高度
						this.navigatorHeight = (menu.top - system.statusBarHeight) * 2 + menu.height + system.statusBarHeight


					}
				})
			},
			choosHandle(item) {
				if (this.chooseAdd) {
					if(this.tabCurrent==0){
						console.log("支付宝");
						uni.$emit('choosAli', item)
					}else{
						console.log("银行卡");
						uni.$emit('choosAddrss', item)
					}
					
					uni.navigateBack()
				}

			},
			card_list() {
				const params = {
					type: 1,
					limit: 50,
					type: this.tabList[this.tabCurrent].id
				}
				card_list(params).then((res) => {
					this.dataList = res.data.list
				})
			},
			openModal(id) {
				this.delCode = id
				this.$refs.modal.open()
			},
			del: debounceImmediate(function() {
				let that = this
				card_del({
					id: this.delCode
				}).then((res) => {
					uni.showToast({
						title: res.msg,
						icon: "none",
						success() {
							that.$refs.modal.close()
							that.card_list()
						}
					});
				}).catch((err) => {
					uni.$u.toast(err)
				})
			}),
			set_default(id) {
				let that = this
				const params = {
					id: id
				}
				default_set(params).then((res) => {
					uni.showToast({
						title: res.msg,
						icon: "none",
						success() {
							that.card_list()
						}
					});
				})
			}

		},
	};
</script>
<style lang="scss" scoped>
	.tab_box {
		position: sticky;
		top: 3rpx;
		height: 100rpx;
		padding: 0 150rpx;
		background: #fff;

		.tab_item {
			font-size: 34rpx;
			height: 100%;
			color: #383838;
			border-bottom: 6rpx solid transparent;

		}

		.active {
			color: #643400;
			border-bottom: 6rpx solid #643400;
		}

	}

	.wrap {
		overflow: hidden;
		width: 750rpx;
		min-height: 100vh;
		position: relative;
		background-color: #F6F7FB;
		padding-bottom: 200rpx;

		.nav_box {

			background: #fff;

			.nav_info {
				font-size: 30rpx;
				height: 88rpx;
				flex-grow: 1;
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 0 27rpx;

				.back_icon {
					display: flex;
					align-items: center;
					justify-content: center;
					width: 32rpx;
					height: 32rpx;

					.iconfont {
						font-size: 26rpx;
					}
				}

				.sys_title {
					color: #383838;
					text-align: center;
				}
			}

		}

		.list-box {
			width: 700rpx;
			overflow: hidden;
			margin: 0 auto;

			.list {
				position: relative;
				width: 100%;
				background: white;
				border-radius: 8rpx;
				padding: 20rpx 30rpx;
				position: relative;
				margin-top: 20rpx;
				overflow: hidden;
				line-height: 50rpx;
				color: #3D3D3D;
				font-size: 28rpx;

				.choos_icon {
					position: absolute;
					font-size: 24rpx;
					right: 30rpx;
					top: 20rpx;
					width: 95rpx;
					height: 44rpx;
					border-radius: 44rpx;
					line-height: 44rpx;
					background: #383838;
					color: #fff;
					text-align: center;
				}

				.name {
					font-weight: 700;

					text {
						margin-left: 20rpx;
					}

					.tip_box {
						margin-left: 20rpx;
						font-size: 24rpx;
						color: #808080;
						width: 177rpx;
						text-align: center;
						border-radius: 4rpx;
						background: #EDEDED;
						height: 37rpx;
						line-height: 37rpx;
					}
				}

				.span {
					color: #757575;
					font-size: 24rpx;

					text {
						color: #3D3D3D;
						margin-right: 20rpx;
					}
				}

				.btn-box {
					border-top: solid 1rpx #F3F4F8;
					padding: 10rpx 0 0 0;
					overflow: hidden;
					width: 100%;
					margin-top: 10rpx;

					.left {
						display: flex;
						align-items: center;
						width: 40%;

						vertical-align: top;

						.img_box {
							width: 34rpx;
							height: 34rpx;
							margin-right: 10rpx;

							image {
								width: 100%;
								height: 100%;

							}
						}

					}

					.right {
						display: flex;

						vertical-align: top;

						.li {
							display: flex;
							align-items: center;
							vertical-align: top;
							margin-left: 20rpx;

							image {
								width: 34rpx;
								height: 34rpx;
								display: inline-block;
								vertical-align: top;
								margin-right: 10rpx;
							}
						}
					}
				}
			}
		}

		.btn-d {
			width: 100%;
			position: fixed;
			bottom: 0;
			left: 0;
			background-color: white;
			padding: 30rpx;
			font-size: 32rpx;
			color: white;
			text-align: center;
			line-height: 90rpx;
			padding-bottom: 50rpx;

			.btn {
				border-radius: 50rpx;
				background: var(--view-theme);
			}
		}
	}
</style>